package cn.stu.framework.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.stu.common.utils.ApiData;
import cn.stu.common.utils.BeanUtils;
import cn.stu.framework.dto.DeptDto;
import cn.stu.framework.dto.DeptPageDto;
import cn.stu.framework.entity.Dept;
import cn.stu.framework.entity.Users;
import cn.stu.framework.mapper.DeptMapper;
import cn.stu.framework.mapper.UsersMapper;
import cn.stu.framework.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.stu.common.enums.DelFlagEnum.USED;


/**
 * 部门服务实施
 *
 * @author 郑志豪
 * @date 2023/09/18
 */
@Service
@AllArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    /**
     * 部门映射器
     */
    private DeptMapper deptMapper;

    private UsersMapper usersMapper;

    /**
     * 选择部门
     *
     * @param deptPageDto 部门Dto
     * @return {@link ApiData}
     */
    @Override
    @Cacheable(value = "deptList", sync = true)
    public ApiData selectDept(DeptPageDto deptPageDto) {
        if (deptPageDto.getLimit() == null && deptPageDto.getPage() == null) {
            List<Dept> deptList = lambdaQuery().list();
            return ApiData.getTrue("获取成功", deptList);
        }
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(deptPageDto.getDeptName()), Dept::getDeptName, deptPageDto.getDeptName());
        Page<Dept> page = new Page<>(deptPageDto.getPage(), deptPageDto.getLimit());
        deptMapper.selectPage(page, wrapper);
        return ApiData.getPageTrue("获取成功", page.getTotal(), page.getRecords());
    }

    /**
     * 删除部门
     *
     * @param deptId 部门ID
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "deptList", allEntries = true)
    public ApiData deleteDept(Long deptId) {
        LambdaQueryWrapper<Users> usersQuery = new LambdaQueryWrapper<>();
        usersQuery.eq(Users::getDeptId, deptId);
        usersQuery.eq(Users::getStatus, USED);
        List<Users> users = usersMapper.selectList(usersQuery);
        if (CollUtil.isNotEmpty(users)) {
            return ApiData.getFalse("该部门还有员工，不能删除！");
        }
        deptMapper.deleteById(deptId);
        return ApiData.getTrue("删除成功！");
    }

    /**
     * 插入部门
     * 新增部门
     *
     * @param dto DTO
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "deptList", allEntries = true)
    public ApiData insertDept(DeptDto dto) {
        Dept dept = BeanUtils.copier(dto, Dept.class);
        deptMapper.insert(dept);
        return ApiData.getTrue("新增成功");

    }

    /**
     * 更新部门
     *
     * @param dto DTO
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "deptList", allEntries = true)
    public ApiData updateDept(DeptDto dto) {
        if (dto.getDeptId() == null) {
            return ApiData.getFalse("参数缺失");
        }
        boolean exists = deptMapper.exists(lambdaQuery().eq(Dept::getDeptId, dto.getDeptId()).getWrapper());
        if (!exists) {
            return ApiData.getFalse("部门不存在");
        }
        Dept dept = BeanUtils.copier(dto, Dept.class);
        deptMapper.updateById(dept);
        return ApiData.getTrue("更新成功");
    }
}
